package com.peanut.mvc.congif;

import com.peanut.mvc.interseptor.TestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import java.util.List;
import java.util.Objects;
import java.util.Properties;

/**
 * @className: com.peanut.mvc.congif-> WebConfig
 * @description: 代替springmvc.xml 配置文件
 * @author: chenhs
 * @createDate: 2021-09-09 22:47
 * @version: 1.0
 * <p>
 * 这个类等同于 springmvc
 * <p>
 * 1文件上传解析器
 * 2异常处理
 * 3拦截器
 * 4扫描组件
 * 5视图解析器
 * 6view-controller 指定跳转路径
 * default-servlet-handler 让静态资源不会被拦截
 */
//标识为注解类
@Configuration
//组件扫描 指定注解生效的范围。
@ComponentScan("com.peanut.*")
//开启注解驱动 这样注解才会生效
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
//    文件上传解析器
    @Bean
    public MultipartResolver multipartResolver(){
        return new CommonsMultipartResolver();
    }
//    异常处理
    @Override
    public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        //  NullPointerException 异常对象 ，跳转的页面。
        Properties properties = new Properties();
        properties.setProperty("NullPointerException", "error");
        SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
        resolver.setExceptionMappings(properties);
       // 存储异常信息的键名 存到sisson中。
        resolver.setExceptionAttribute("ex");
        resolvers.add(resolver);
    }

    //    等同于 default-servlet-handler 作用就是开启静态资源的访问
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    //拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//     addPathPatterns 添加拦截请求  /**拦截所有
        registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**");
    }
// 相当于  <mvc:view-controller path="/" view-name="index"/>

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/pageSkipe").setViewName("skipeTargetPage");
    }

    /**
     * @description 配置模板解析器
     * @author cehnhs
     * @updateTime 2021/9/19 8:56
     */
    @Bean
    public ITemplateResolver templateResoler() {
        // context为根应用上下文执行初始化工作
        WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
        assert context != null;
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(Objects.requireNonNull(context.getServletContext()));
//      路径前缀
        resolver.setPrefix("/WEB-INF/templates/");
//      路径后缀
        resolver.setSuffix(".html");
//        配置返回数据的编码格式
        resolver.setCharacterEncoding("UTF-8");
//        模板引擎 默认是 html
        resolver.setTemplateMode(TemplateMode.HTML);
        return resolver;
    }

    /**
     * @description 生成模板引擎并伪模板引擎注入模板解析器
     * @author cehnhs
     * @updateTime 2021/9/19 8:55
     */
    @Bean
    public SpringTemplateEngine templateEngine(ITemplateResolver resolver) {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(resolver);
        return engine;
    }

    /**
     * @description 方法注释
     * @author cehnhs
     * @updateTime 2021/9/19 8:58
     */
    @Bean
    public ViewResolver viewResolver(SpringTemplateEngine engine) {
        ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
        thymeleafViewResolver.setCharacterEncoding("UTF-8");
        thymeleafViewResolver.setTemplateEngine(engine);
        return thymeleafViewResolver;
    }
}

